import 'package:json_annotation/json_annotation.dart';
import 'package:flutter_account_book/utils/db/db_helper.dart';

part 'account_book.g.dart';

@JsonSerializable()
class AccountBook extends DbModel {
  static const int idAll = -100;

  @JsonKey(required: true)
  late int id = 0;
  @JsonKey(required: true)
  late String name;
  @JsonKey(defaultValue: 0)
  late int order = 0;
  // 是否同步到远端
  @JsonKey(defaultValue: 0)
  late int asyncService = 0;
  @JsonKey(defaultValue: 0)
  late int deleted = 0;

  AccountBook() : super.fromJson({});

  factory AccountBook.fromJson(data) => _$AccountBookFromJson(data);

  @override
  Map<String, dynamic> toJson() => _$AccountBookToJson(this);

  save() async {
    bool has = await DbHelper.helper.accountBookDao.has(id);
    if (!has) {
      await DbHelper.helper.accountBookDao.insert(this);
    } else {
      await DbHelper.helper.accountBookDao.update(this);
    }
  }
}
